# joint minimization of the training error term and the trp term
#theja 2010-July-14 : start.

#1. parameters related to term 2
param N > 0, integer; # number of test nodes
set V := 1..N; # enumerating the test nodes as Vertices
param d{V,V} >= 0; # creating an input ditance matrix of the size (test vertices) ^2.
#2. parameters related to term 1
#param dim > 0, integer; # dimension of the parameter vector lambda, N for N-1 D data.
#3. parameter weigning term1 wrt term2
#param C >0; #weight between term 1 and term 2.


#1. variables for term 2
var y{V,V} >=0 binary; # binary on-off variables indicating whether the edge exists in the solution.
var z{V,V} >=0 integer; # flow variables indicating the flow on each edge.
#2. variables for term 1
#var lambda{dim}; # the parameters of the probability estimator.

#objective:
minimize totalwaitingtime : sum{i in V, j in V} d[i,j] * z[i,j]; #CH currently only the TRP cost here.

#constraints:
subject to no_self_loop1 {i in V}: y[i,i] = 0; # No edge from node i to itself
subject to no_self_loop2 {i in V}: z[i,i] = 0; # No flow from node i to itself
subject to successor {i in V} : sum{j in V} y[i,j] = 1; # Exactly one edge out from each node
subject to predecessor {j in V} : sum{i in V} y[i,j] = 1;# Exactly one edge into each node
subject to flow_comming_back_to_node_1: sum{i in V} z[i,1] = 1; #CH #Flow coming back to initial at end of the loop is p(1)
subject to flow_changes {k in V:k !=1}: sum{i in V} z[i,k] - sum{j in V} z[k,j] = 1; #CH
subject to one_more_flow_change {k in V: k==1}: sum{i in V} z[i,k] - sum{j in V} z[k,j] = 1 - N; #CH
#Change of flow after crossing node k is either p(k) or it is the sum of p’s minus p(1)
subject to relation_btw_y_z {i in V,j in V: i!=1 && j!=1}: z[i,j] <= (N-1)*y[i,j]; #Connects flows z to indicators of edge y
subject to relation_btw_y_z_1 {i in V}: z[i,1] <= 1*y[i,1];
subject to relation_btw_y_z_2 {j in V}: z[1,j] <= N*y[1,j];

